package com.shouhuyun.controller;
/*/
    预约设置
 */

import com.alibaba.dubbo.config.annotation.Reference;
import com.shouhuyun.constant.MessageConstant;
import com.shouhuyun.entity.Result;
import com.shouhuyun.pojo.OrderSetting;
import com.shouhuyun.service.OrderSettingService;
import com.shouhuyun.utils.POIUtils;
import com.sun.webkit.dom.XPathNSResolverImpl;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@RequestMapping("/ordersetting")
@RestController
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;
    /**
     * Excel文件上传，并解析文件内容保存到数据库
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
//            读取Excel文件数据
//            list集合的每一个对象是一个数组  存放的是某一行的数据
//            数组中的每个对象是 每一个单元格的数据
            List<String[]> list = POIUtils.readExcel(excelFile);
            List<OrderSetting> orderSettingList = new ArrayList<>();

            if (list != null && list.size() >0){
                for (String[] strings : list) {
                    OrderSetting orderSetting = new OrderSetting(
//                            日期
                            new Date(strings[0]),
//                            预约人数
                            Integer.parseInt(strings[1])
                    );
                    orderSettingList.add(orderSetting);
                }
//              通过dubbo远程调用服务实现数据批量导入到数据库
                orderSettingService.add(orderSettingList);
            }
        }catch (IOException e){
            e.printStackTrace();
//            文件解析失败
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }


    //日历控件显示 数据
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){  //参数格式 2019-12
        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            //获取预约设置数据成功
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
//            获取预约设置数据失败
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }

    }

//    根据日期设置对应的预约设置数据
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}




















